package mapEditor.obj;

import debug.*;

import mapEditor.MapEditor;
import mapEditor.map.ObjectModel;
import mapEditor.utils.MapLocation;
import mapEditor.utils.FillStyle;
import mapEditor.utils.StrokeStyle;
import mapEditor.utils.Util;

import org.json.JSONObject;
import org.json.JSONArray;
import org.json.JSONException;

import java.awt.GridLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JCheckBox;
import javax.swing.JTextField;
import javax.swing.JPanel;


public class PathObject extends DrawObject
{ private static final String TAG="mapEditor.obj.PathObject";
//  private static final boolean VERBOSE=true;
  private static final boolean DEBUG=true;

  public static final String TYPE="Path";

  public PathObject(ObjectModel objectModel,MapLocation location) throws JSONException
  { super(objectModel,null);
    stroke.setEnabled(true);
    add(location); 
  }
  
  public PathObject(ObjectModel objectModel,JSONObject jsonObject) throws JSONException
  { super(objectModel,jsonObject.getString("id"));
    if(DEBUG) Log.d(TAG,"decodeSVG: jsonObject="+jsonObject);
    this.fill=new FillStyle(jsonObject);
    this.stroke=new StrokeStyle(jsonObject);
    boolean closed=jsonObject.optBoolean("closed",false);
    JSONArray d=jsonObject.getJSONArray("d");
    //if(DEBUG) Log.d(TAG,"PathObject.decodeSVG: name="+name+", d="+d);
    clearPointSet();
    for(int i=0;i<d.length();i++)
    { JSONArray p=d.getJSONArray(i);
      add(MapLocation.newPixelPoint(objectModel,p));
    }
    setClosedPath(closed);
  }

  public JSONObject toJSONObject() throws JSONException
  { JSONObject jsonObject=new JSONObject();
    jsonObject.put("type",TYPE);
    jsonObject.put("id",getIdent());
    jsonObject.put("id",getIdent());
    jsonObject.put("closed",getClosedPath());  
    jsonObject.put("d",getD());
    jsonObject.put("fill",fill.encode());
    jsonObject.put("stroke",stroke.encode());
	return(jsonObject);
  }
  
  private JSONArray getD() throws JSONException
  { JSONArray arr=new JSONArray();
    int n=numberOfPoints();
    for(int i=0;i<n;i++)
    { MapLocation p=getMapLocation(i);
      arr.put(p.encode());
    }
	return(arr);  
  }
    
  private void ensureParallelogram()
  { if(this.numberOfPoints()==4)
    { MapLocation p0=getMapLocation(0);
      MapLocation p1=getMapLocation(1);
      MapLocation p2=getMapLocation(2);
      MapLocation p3=getMapLocation(3);
      double dx=p1.getX()-p0.getX();
      double dy=p1.getY()-p0.getY();
      p3.setX(p2.getX()-dx);
      p3.setY(p2.getY()-dy);
    }
  }
  
  private void ensureRectangle()
  { if(numberOfPoints()==4)
    { MapLocation p0=getMapLocation(0);
      MapLocation p1=getMapLocation(1);
      MapLocation p2=getMapLocation(2);
      double dx=p1.getX()-p0.getX();
      double dy=p1.getY()-p0.getY();
      double a=p2.getY()-p1.getY();      
      double b=dy/dx*a;
      p2.setX(p1.getX()-b);
      ensureParallelogram();
    }
  }


  // ****************************************************************
  // *** INNER CLASS: EditGeneralPanel
  // ****************************************************************
  public JPanel getEditGeneralPanel(String title)
  { return(new EditGeneralPanel(title)); }
  private class EditGeneralPanel extends JPanel
  { static final long serialVersionUID=0; // AD'HOC
    private JTextField identField;
    private JButton strokeButton;
    private JButton fillButton;
      
    public void paint(Graphics g)
    { if(DEBUG) Log.d(TAG,"EditGeneralPanel.paint: ");
      identField.setText(getIdent());
      strokeButton.setBackground(stroke.getColor());
      fillButton.setBackground(fill.getColor());
  	  super.paint(g);  
    }
    	
    public EditGeneralPanel(String title)
    { setBorder(Util.getTitleBorder(title));
      if(numberOfPoints()==4) // Firkant
      { setPreferredSize(new Dimension(350,120));//140));
        setLayout(new GridLayout(6,2));
      }
      else
      { setPreferredSize(new Dimension(350,120));
        setLayout(new GridLayout(5,2));
      }
      add(new JLabel("       Ident:"));
      identField=new JTextField(getIdent());
      identField.addActionListener(new ActionListener()
      { public void actionPerformed(ActionEvent e)
        { setIdent(identField.getText()); }
      });
      add(identField);
      fillButton=addEditFillButton(this,true);
      strokeButton=addEditStrokeButton(this,true);
        
      JCheckBox closedBox=new JCheckBox("Closed Path",null,getClosedPath());
      closedBox.addActionListener(new ActionListener()
      { public void actionPerformed(ActionEvent e)
        { setClosedPath(!getClosedPath());
 	      MapEditor.repaint();        	
        }
      });
      add(closedBox);
      add(new JLabel(" ")); // Space only
      
      if(numberOfPoints()==4)
      { JButton parallelogramButton=new JButton("Adjust Parallelogram");
//      parallelogramButton.setBackground(fill.getColor());
        parallelogramButton.addActionListener(new ActionListener()
        { public void actionPerformed(ActionEvent e) { ensureParallelogram(); } });
        add(parallelogramButton);
        
        JButton rectangleButton=new JButton("Adjust Rectangle");
//      rectangleButton.setBackground(fill.getColor());
        rectangleButton.addActionListener(new ActionListener()
        { public void actionPerformed(ActionEvent e) { ensureRectangle(); } });
        add(rectangleButton);
      }
    }
  }
  
}
